﻿// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Windows.Forms;
using OpenNETCF.Windows.Forms;
using svs.Mobile.BeyondMedia.FeedCore;

namespace svs.Mobile.BeyondMedia.Forms.Settings
{
	public partial class FeedContentSettings : UserControl, ISettingsPage
	{
		public FeedContentSettings()
		{
			KeyValuePair<string, string>[] fontSizes = {
			                                           	new KeyValuePair<string, string>("Largest", "x-large"),
			                                           	new KeyValuePair<string, string>("Larger", "large"),
			                                           	new KeyValuePair<string, string>("Medium", "medium"),
			                                           	new KeyValuePair<string, string>("Smaller", "small"),
			                                           	new KeyValuePair<string, string>("Smallest", "x-small")
			                                           };


			KeyValuePair<string, int>[] autocollapse = {
			                                           	new KeyValuePair<string, int>("Collapse Always", 0),
			                                           	new KeyValuePair<string, int>("Longer than 100 symbols", 100),
			                                           	new KeyValuePair<string, int>("Longer than 300 symbols", 300),
			                                           	new KeyValuePair<string, int>("Longer than 500 symbols", 500),
			                                           	new KeyValuePair<string, int>("Longer than 800 symbols", 800),
			                                           	new KeyValuePair<string, int>("Longer than 1000 symbols", 1000),
			                                           	new KeyValuePair<string, int>("Never Collapse", -1),
			                                           	new KeyValuePair<string, int>("Collapse Only Read Items", -2),
			                                           };
			InitializeComponent();

			AutoCollapse.DisplayMember = "Key";
			AutoCollapse.ValueMember = "Value";
			AutoCollapse.DataSource = autocollapse;

			FontSize.DisplayMember = "Key";
			FontSize.ValueMember = "Value";
			FontSize.DataSource = fontSizes;

			FormatProvider.DisplayMember = "Key";
			FormatProvider.ValueMember = "Value";
			FormatProvider.DataSource = Configuration.LinkPrefixProviders;

		}

		public static string PageName
		{
			get { return "Feed Content Settings"; }
		}

		public static string PageDescription
		{
			get
			{
				return "Set Default Browser, Auto collapse level... ";
			}
		}


		public static SettingsPageHolderForm.SettingsPages PageID
		{
			get { return SettingsPageHolderForm.SettingsPages.FeedContent; }
		}

		#region ISettingsPage Members

		public bool CheckAndSaveSettings()
		{
			Configuration.DefaultDocFontSize = (string) FontSize.SelectedValue;
			Configuration.AutoCollapseLevel = (int) AutoCollapse.SelectedValue;
			Configuration.LinkPrefix = (string) FormatProvider.SelectedValue;
			Configuration.HideReadItems = HideReadItems.Checked;

			return true;
		}

		public void LoadPageSettings()
		{
			FontSize.SelectedValue = Configuration.DefaultDocFontSize;
			AutoCollapse.SelectedValue = Configuration.AutoCollapseLevel;
			FormatProvider.SelectedValue = Configuration.LinkPrefix;
			HideReadItems.Checked = Configuration.HideReadItems;

			OpenLinksIn.SelectedIndex = string.IsNullOrEmpty(Configuration.ExternalUrlHandler) ? 0 : 1;
			OnOpenLinksInChanged(null, null);
		}

		public bool ShouldReloadRepositoryOnDone
		{
			get { return false; }
		}

		#endregion

		private void OnOpenLinksInChanged(object sender, System.EventArgs e)
		{
			switch(OpenLinksIn.SelectedIndex)
			{
				case 0:
					SetDefaultBrowser.Enabled = false;
					Configuration.ExternalUrlHandler = string.Empty;
					break;

				case 1:
					SetDefaultBrowser.Enabled = true;
					if (string.IsNullOrEmpty(Configuration.ExternalUrlHandler))
						Configuration.ExternalUrlHandler = Configuration.GetDefaultHttpHandlerCommandLine();
					break;
			}
		}

		private void OnSelectDefaultBrowserClick(object sender, System.EventArgs e)
		{
			string cmdLine = InputBoxEx.ShowInputBox("Configure Browser",
			                                         "Browser Command Line",
																							 Configuration.ExternalUrlHandler,
			                                         "Enter a command line you want to use to start the browser. Use %1 to indicate the place of the URL.",
			                                         OnValidateCommandLine,
																							 "Command line must contain an executabe name (.exe) and %1 to indicate the URL prameter",
																							 "Invalid Command Line");
			if(cmdLine != null)
			{
				Configuration.ExternalUrlHandler = cmdLine;  
			}
		}

		private bool OnValidateCommandLine(string input)
		{
			if(input.IndexOf(".exe",StringComparison.InvariantCultureIgnoreCase) <= 0)
				return false;

			if (input.IndexOf("%1", StringComparison.InvariantCultureIgnoreCase) == -1)
				return false;

			return true;
		}
	}
}